package com.lw.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lw.common.IJsonResult;
import com.lw.mapper.VideoInfoMapper;
import com.lw.pojo.Bgm;
import com.lw.pojo.VideoInfo;
import com.lw.utils.PagedResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Service("VideoServiceImpl")
@Slf4j
public class VideoServiceImpl {

    private String ffmpegEXE = "D:\\softwares\\ffmpeg\\ffmpeg-7.0.2-essentials_build\\bin\\ffmpeg.exe";

    @Autowired
    private VideoInfoMapper videoInfoMapper;

    public Bgm queryBgmById(String bgmId,String operatorType){
        if("add".equals(operatorType)){
            //调用远程服务器查询数据
        }else if ("delete".equals(operatorType)){
            //调用本地数据库查询信息
        }
        return new Bgm();
    }

    /**
     *
     * @param page 当前页数
     * @param pageSize 每页数据
     * @param param 查询参数
     * @return
     */
    public PagedResult queryAllVideos(Integer page,Integer pageSize,VideoInfo param){

        //分页
        PageHelper.startPage(page,pageSize);

        List<VideoInfo> videoInfos  = this.videoInfoMapper.queryAllVideos(param);

        PageInfo<VideoInfo> pageList = new PageInfo<>(videoInfos);

        PagedResult pagedResult = new PagedResult();

        pagedResult.setPage(page);//当前页
        pagedResult.setTotal(pageList.getPages()); //总页数
        pagedResult.setRows(videoInfos);//结果集
        pagedResult.setRecords(pageList.getTotal());//总记录数

        //下面会查询全部数据，并不会被上面分页语句影响
//        List<VideoInfo> videoInfos2  = this.videoInfoMapper.queryAllVideos(param);
//        log.info("分页数据："+videoInfos2.size());

        return pagedResult;

    }

    /**
     * 获取截图
     * @param videoInputPath
     * @param coverOutputPath
     */
    public void getCover(String videoInputPath, String coverOutputPath)  {
//		ffmpeg -ss 00:00:01 -y -i 视频.mp4 -vframes 1 cover.jpg
        /*
        -ss 00:00:01 获取视频01秒时的图片 ,
        -vframes 1获取一帧，
        -y 是可以重复生成图片，新图片会覆盖老图片
         */
        List<String> converCommand = new java.util.ArrayList<String>();
        converCommand.add(ffmpegEXE);

        // 指定截取第1秒
        converCommand.add("-ss");
        converCommand.add("00:00:01");

        converCommand.add("-y");
        converCommand.add("-i");
        converCommand.add(videoInputPath);

        converCommand.add("-vframes");
        converCommand.add("1");

        converCommand.add(coverOutputPath);

        ProcessBuilder processBuilder = new ProcessBuilder(converCommand);
        try {
            Process process = processBuilder.start();

            //处理异常，否则出错就卡主了
            InputStream errorStream = process.getErrorStream();
            InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
            BufferedReader br = new BufferedReader(inputStreamReader);

            String line = null;
            while ((line=br.readLine())!=null){

            }
            br.close();
            inputStreamReader.close();
            errorStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 音视频合成新视频
     * @Param videoInputSeconds 原视频时长 单位秒
     */
    public void mergeAudioVideo(String videoInputPath,String audioInputPath,
                                String videoOutputPath,double videoInputSeconds){
        //命令  ffmpeg -i  视频.mp4  -i  音频.mp3 -t 7 -y 新的视频.mp4
        List<String> mergeCommand = new ArrayList<>();
        mergeCommand.add(ffmpegEXE);
        mergeCommand.add("-i");
        mergeCommand.add(videoInputPath);
        mergeCommand.add("-i");
        mergeCommand.add(audioInputPath);
        mergeCommand.add("-t");
        mergeCommand.add(""+videoInputSeconds);
        mergeCommand.add("-y");
        mergeCommand.add(videoOutputPath);

        ProcessBuilder processBuilder = new ProcessBuilder(mergeCommand);
        try {
            Process process = processBuilder.start();

            //处理异常，否则出错就卡主了
            InputStream errorStream = process.getErrorStream();
            InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
            BufferedReader br = new BufferedReader(inputStreamReader);

            String line = null;
            while ((line=br.readLine())!=null){

            }
            br.close();
            inputStreamReader.close();
            errorStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 视频转换
     * @param videoInputPath
     * @param videoOutputPath
     */
    public void convertVideo(String videoInputPath,String videoOutputPath){
        //视频转换
        //命令  ffmpet.exe -i cc.mp4 dd.avi
        List<String> convertCommand = new ArrayList<>();
        convertCommand.add(ffmpegEXE);
        convertCommand.add("-i");
        convertCommand.add(videoInputPath);
        convertCommand.add(videoOutputPath);

        ProcessBuilder processBuilder = new ProcessBuilder(convertCommand);
        try {
            Process process = processBuilder.start();

            //处理异常，否则出错就卡主了
            InputStream errorStream = process.getErrorStream();
            InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
            BufferedReader br = new BufferedReader(inputStreamReader);

            String line = null;
            while ((line=br.readLine())!=null){

            }
            br.close();
            inputStreamReader.close();
            errorStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        VideoServiceImpl videoService = new VideoServiceImpl();

        String videoInputPath = "E:\\temp\\user\\files\\videos\\cc.mp4";
        String videoOutputPath = "E:\\temp\\user\\files\\videos\\dd.avi";
        try {
            videoService.convertVideo(videoInputPath,videoOutputPath);
        } catch (RuntimeException e) {
        }

    }


}
